﻿using Nop.Plugin.Misc.MessageDesigner.Controllers;
using Nop.Plugin.Misc.MessageDesigner.Services;
using Nop.Services.Events;
using Nop.Services.Localization;
using Nop.Services.Messages;
using Nop.Services.Security;
using Nop.Services.Stores;
using Nop.Web.Framework.Events;
using System;
using System.Text;
using System.Web.Mvc;

namespace Nop.Plugin.Misc.MessageDesigner.Events
{
    /// <summary>
    /// This class is used to detect when a Product is being edited in order to add a new tab for our encryption key.
    /// These have to be unique for each Product so that someone doesn't purchase a license for one Product and then use it for others
    /// </summary>
    public class MessageDesignerCostumer : IConsumer<AdminTabStripCreated>
    {
        private readonly IMessageDesignerService _messageDesignService;
        private readonly ILanguageService _languageService;
        private readonly ILocalizedEntityService _localizedEntityService;
        private readonly ILocalizationService _localizationService;
        private readonly IPermissionService _permissionService;
        private readonly IStoreService _storeService;
        private readonly IStoreMappingService _storeMappingService;
        private readonly IMessageTemplateService _messageTemplateService;

        public MessageDesignerCostumer(IMessageDesignerService messageDesignService,
            ILanguageService languageService,
            ILocalizedEntityService localizedEntityService,
            ILocalizationService localizationService,
            IPermissionService permissionService, IStoreService storeService,
            IStoreMappingService storeMappingService)
        {
            this._messageDesignService = messageDesignService;
            this._languageService = languageService;
            this._localizedEntityService = localizedEntityService;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._storeService = storeService;
            this._storeMappingService = storeMappingService;
        }

        public void HandleEvent(AdminTabStripCreated eventMessage)
        {
            if (eventMessage.TabStripName == "template-edit")
            {
                MiscMessageDesignerController controller = new MiscMessageDesignerController(_messageDesignService, _languageService,_localizedEntityService, _localizationService,
                                                                                               _permissionService, _storeService, _storeMappingService, _messageTemplateService);

                int templateId = controller.GetTemplateId();
                string url = "/MiscMessageDesigner/AddTemplateTab?templateId=" + templateId;
                string tabName = _localizationService.GetResource("Plugins.Misc.MessageDesigner");
                var sb = new StringBuilder();

                sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
                sb.Append(Environment.NewLine);
                sb.Append("$(document).ready(function () {");
                sb.Append(Environment.NewLine);
                sb.Append("var kTabs = $('#template-edit').data('kendoTabStrip');");
                sb.Append(Environment.NewLine);
                sb.Append(" kTabs.append({ text: \"" + tabName + "\", contentUrl: \"" + url + "\" });");
                sb.Append(Environment.NewLine);
                sb.Append("});");
                sb.Append(Environment.NewLine);
                sb.Append("</script>");
                sb.Append(Environment.NewLine);
                eventMessage.BlocksToRender.Add(MvcHtmlString.Create(sb.ToString()));
   
            }
        }
    }
}
